package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.TopDao;
import com.example.spark.entity.Top;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.TopService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

@Service
public class TopServiceImpl extends ServiceImpl<TopDao, Top> implements TopService {
    @Autowired
    private TopDao topDao;

    @Resource
    MinioCloudStorageService minIoService;

    /**
     * 查询所有头部信息
     *
     * @return List<Top>
     */
    @Override
    public List<Top> getAll() {
        return topDao.selectList(null);
    }

    /**
     * 添加头部
     *
     * @param top 头部
     * @return CommonResult
     */
    @Override
    public CommonResult add(Top top) {
        Integer num = topDao.selectCount(null);
        if (num >= 1) {
            return new CommonResult<>(200, "头部已存在");
        }
        LambdaQueryWrapper<Top> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Top::getTopName, top.getTopName());
        Top top1 = topDao.selectOne(lqw);
        if (top1 != null) {
            return new CommonResult<>(200, "头部名称已存在");
        }
        lqw.eq(Top::getTopLogo, top.getTopLogo());
        Top top2 = topDao.selectOne(lqw);
        if (top2 != null) {
            return new CommonResult<>(200, "头部logo已存在");
        }
        top.setTopLogo("000");
        top.setId(getId());
        topDao.insert(top);
        return new CommonResult<>(200, "success");
    }

    /**
     * 删除头部
     *
     * @param id 头部id
     * @return ComonResult
     */
    @Override
    public CommonResult deleteById(Long id) {
        Top top = topDao.selectById(id);
        if (top == null) {
            return new CommonResult<>(200, "该头部不存在");
        }
        topDao.deleteById(id);
        return new CommonResult<>(200, "success");
    }

    /**
     * 更新头部
     *
     * @param top 头部
     * @return CommonResult
     */
    @Override
    public CommonResult update(Top top) {
        Top top1 = topDao.selectById(top.getId());
        if (top1 == null) {
            return new CommonResult<>(200, "该头部不存在");
        }
        LambdaQueryWrapper<Top> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Top::getTopName, top.getTopName());
        Top top2 = topDao.selectOne(lqw);
        if (top2 != null) {
            return new CommonResult<>(200, "头部名称已存在");
        }
        lqw.eq(Top::getTopLogo, top.getTopLogo());
        Top top3 = topDao.selectOne(lqw);
        if (top3 != null) {
            return new CommonResult<>(200, "头部logo已存在");
        }
        topDao.updateById(top);
        return new CommonResult<>(200, "success");

    }

    /**
     * 添加/修改头部图片
     *
     * @param file 图片文件
     * @param id   头部id
     * @return CommonResult
     * @throws IOException
     */
    @Override
    public CommonResult addPicture(MultipartFile file, Long id) throws IOException {
        Top top = topDao.selectById(id);
        if (top == null) {
            return new CommonResult<>(200, "该头部不存在");
        }
        // getOriginalFileName方法获取的是文件的完整名称，包括文件名称+文件拓展名
        //后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        // 上传文件并获取文件存储路径
        String url = minIoService.uploadPhotoSuffix(file.getBytes(), extension, top.getTopName());
        top.setTopLogo(url);
        topDao.updateById(top);
        return new CommonResult<>(200, "success");
    }
}
